function ShowRankBar(node_id, rank_count, rank_score, pv) {
  $('#' + node_id).html('<div class="rank-plot" style="background-position:' 
      + (rank_score / 5.0 - 1.0) * 100 + 'px 50%"></div><div class="rank-desc"><span id="rank-score">'
      + rank_score + '</span>分(共<span id="rank-count">' + rank_count 
      + '</span>人评分)</div><div id="rank-icons"></div>');

  var rank_icons = $('#rank-icons');
  for(var i = 1; i < 6; ++i) {
    rank_icons.append($('<input class="rankicon" value="' + i + '" title="' + i + '分"></input>'));
  }

  $('.rankicon', rank_icons).click(
    function() {
      var p = $(this).parent();
      $('.rankicon').remove();
      p.append($('<span id="rank-state" style="color:#c36;">正在提交...</span>'));
      var score = $(this).val();
      $.ajax('/cgi/stat-rank?aid=' + article_id + '&score=' + score,
        {
          error : function(msg) {
            $('#rank-state').text('提交失败');
          },
          success : function(msg) {
            var rank = $.parseJSON(msg);
            $('#rank-count').text(rank['count']);
            $('#rank-score').text(rank['score']);
            $('#rank-state').text('评分成功！(' + score + '分)');
            var offset = (rank['score'] / 5.0 - 1.0) * 100;
            $('div.rank-plot').css({"background-position": offset + 'px 50%'});
          }
        }
      );
    }
  );

  $('.rankicon', rank_icons).hover(
    function() {
     var score = $(this).val();
     $('.rankicon').each(function() {
       var kid = $(this);
       if (kid.val() <= score) {
         kid.css('opacity', '1.0');
         kid.css('filter', 'alpha(opacity=100)');
       }
     });
    },
    function() {
      var score = $(this).val();
      $('.rankicon').each(function() {
          var kid = $(this);
          if (kid.val() <= score) {
            kid.css('opacity', '.2');
            kid.css('filter', 'alpha(opacity=20)');
          }
        }
      );
    }
  );
}
$(document).ready(
  function() {
    $.ajax('/cgi/stat-load?aid=' + article_id, 
      {
        success : function(msg) {
          var rank = $.parseJSON(msg);
          ShowRankBar('rank-bar', rank['count'], rank['score']);
        }
      }
    );
  }
);
